home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch7 / Resize.bas < prev    next >
Encoding:
BASIC Source File  |  1999-05-05  |  8.0 KB  |  235 lines

  1. Attribute VB_Name = "Resize"
  2. Option Explicit
  3.  
  4. ' Shrink or enlarge the picture.
  5. Public Sub ResizePicture(ByVal pic_from As PictureBox, ByVal pic_to As PictureBox, ByVal from_xmin As Single, ByVal from_ymin As Single, ByVal from_wid As Single, ByVal from_hgt As Single, ByVal to_xmin As Single, ByVal to_ymin As Single, ByVal to_wid As Single, ByVal to_hgt As Single)
  6. Dim x_scale As Single
  7. Dim y_scale As Single
  8.  
  9.     ' If either scale is less than 1, use ShrinkPicture
  10.     If (to_wid / from_wid < 1#) Or _
  11.        (to_hgt / from_hgt < 1#) _
  12.     Then
  13.         ' Shrink the picture.
  14.         ShrinkPicture pic_from, pic_to, _
  15.             from_xmin, from_ymin, _
  16.             from_wid, from_hgt, _
  17.             to_xmin, to_ymin, _
  18.             to_wid, to_hgt
  19.     Else
  20.         ' Enlarge the picture.
  21.         EnlargePicture pic_from, pic_to, _
  22.             from_xmin, from_ymin, _
  23.             from_wid, from_hgt, _
  24.             to_xmin, to_ymin, _
  25.             to_wid, to_hgt
  26.     End If
  27. End Sub
  28. ' Shrink the image.
  29. Private Sub ShrinkPicture(ByVal pic_from As PictureBox, ByVal pic_to As PictureBox, ByVal from_xmin As Single, ByVal from_ymin As Single, ByVal from_wid As Single, ByVal from_hgt As Single, ByVal to_xmin As Single, ByVal to_ymin As Single, ByVal to_wid As Single, ByVal to_hgt As Single)
  30. Dim x_scale As Single
  31. Dim y_scale As Single
  32. Dim white_pixel As RGBTriplet
  33. Dim input_pixels() As RGBTriplet
  34. Dim result_pixels() As RGBTriplet
  35. Dim bits_per_pixel As Integer
  36. Dim ix_max As Single
  37. Dim iy_max As Single
  38. Dim x_in As Single
  39. Dim y_in As Single
  40. Dim ix_out As Integer
  41. Dim iy_out As Integer
  42. Dim ix_in As Integer
  43. Dim iy_in As Integer
  44. Dim x1 As Single
  45. Dim x2 As Single
  46. Dim y1 As Single
  47. Dim y2 As Single
  48. Dim X As Integer
  49. Dim Y As Integer
  50. Dim r As Integer
  51. Dim g As Integer
  52. Dim b As Integer
  53. Dim num_pixels As Integer
  54.  
  55.     ' Set the white pixel's value.
  56.     With white_pixel
  57.         .rgbRed = 255
  58.         .rgbGreen = 255
  59.         .rgbBlue = 255
  60.     End With
  61.  
  62.     ' Get the pixels from pic_from.
  63.     GetBitmapPixels pic_from, input_pixels, bits_per_pixel
  64.  
  65.     ' Get the pixels from pic_to.
  66.     GetBitmapPixels pic_to, result_pixels, bits_per_pixel
  67.  
  68.     ' Get the original image's bounds.
  69.     ix_max = pic_from.ScaleWidth - 2
  70.     iy_max = pic_from.ScaleHeight - 2
  71.  
  72.     ' Calulate the mapping values.
  73.     from_xmin = from_xmin
  74.     from_ymin = from_ymin
  75.     to_xmin = to_xmin
  76.     to_ymin = to_ymin
  77.     x_scale = to_wid / (from_wid - 1)
  78.     y_scale = to_hgt / (from_hgt - 1)
  79.  
  80.     ' Calculate the output pixel values.
  81.     For iy_out = 0 To pic_to.ScaleHeight - 1
  82.         For ix_out = 0 To pic_to.ScaleWidth - 1
  83.             ' Map the pixel value from
  84.             ' (ix_out, iy_out) to (x_in, y_in).
  85.             x1 = Int(from_xmin + (ix_out - to_xmin) / x_scale)
  86.             x2 = Int(from_xmin + (ix_out + 1 - to_xmin) / x_scale) - 1
  87.             y1 = Int(from_ymin + (iy_out - to_ymin) / y_scale)
  88.             y2 = Int(from_ymin + (iy_out + 1 - to_ymin) / y_scale) - 1
  89.  
  90.             ' Average the pixels in this area.
  91.             r = 0
  92.             g = 0
  93.             b = 0
  94.             For X = x1 To x2
  95.                 For Y = y1 To y2
  96.                     With input_pixels(X, Y)
  97.                         r = r + .rgbRed
  98.                         g = g + .rgbGreen
  99.                         b = b + .rgbBlue
  100.                     End With
  101.                 Next Y
  102.             Next X
  103.  
  104.             ' Save the result.
  105.             num_pixels = (x2 - x1 + 1) * (y2 - y1 + 1)
  106.             With result_pixels(ix_out, iy_out)
  107.                 .rgbRed = r / num_pixels
  108.                 .rgbGreen = g / num_pixels
  109.                 .rgbBlue = b / num_pixels
  110.             End With
  111.         Next ix_out
  112.     Next iy_out
  113.  
  114.     ' Set pic_to's pixels.
  115.     SetBitmapPixels pic_to, bits_per_pixel, result_pixels
  116.     pic_to.Picture = pic_to.Image
  117. End Sub
  118. ' Enlarge the image.
  119. Private Sub EnlargePicture(ByVal pic_from As PictureBox, ByVal pic_to As PictureBox, ByVal from_xmin As Single, ByVal from_ymin As Single, ByVal from_wid As Single, ByVal from_hgt As Single, ByVal to_xmin As Single, ByVal to_ymin As Single, ByVal to_wid As Single, ByVal to_hgt As Single)
  120. Dim x_scale As Single
  121. Dim y_scale As Single
  122. Dim white_pixel As RGBTriplet
  123. Dim input_pixels() As RGBTriplet
  124. Dim result_pixels() As RGBTriplet
  125. Dim bits_per_pixel As Integer
  126. Dim ix_max As Single
  127. Dim iy_max As Single
  128. Dim x_in As Single
  129. Dim y_in As Single
  130. Dim ix_out As Integer
  131. Dim iy_out As Integer
  132. Dim ix_in As Integer
  133. Dim iy_in As Integer
  134. Dim dx As Single
  135. Dim dy As Single
  136. Dim dx1 As Single
  137. Dim dx2 As Single
  138. Dim dy1 As Single
  139. Dim dy2 As Single
  140. Dim v11 As Integer
  141. Dim v12 As Integer
  142. Dim v21 As Integer
  143. Dim v22 As Integer
  144.  
  145.     ' Set the white pixel's value.
  146.     With white_pixel
  147.         .rgbRed = 255
  148.         .rgbGreen = 255
  149.         .rgbBlue = 255
  150.     End With
  151.  
  152.     ' Get the pixels from pic_from.
  153.     GetBitmapPixels pic_from, input_pixels, bits_per_pixel
  154.  
  155.     ' Get the pixels from pic_to.
  156.     GetBitmapPixels pic_to, result_pixels, bits_per_pixel
  157.  
  158.     ' Get the original image's bounds.
  159.     ix_max = pic_from.ScaleWidth - 2
  160.     iy_max = pic_from.ScaleHeight - 2
  161.  
  162.     ' Calulate the mapping values.
  163.     from_xmin = from_xmin
  164.     from_ymin = from_ymin
  165.     to_xmin = to_xmin
  166.     to_ymin = to_ymin
  167.     x_scale = to_wid / (from_wid - 1)
  168.     y_scale = to_hgt / (from_hgt - 1)
  169.  
  170.     ' Calculate the output pixel values.
  171.     For iy_out = 0 To pic_to.ScaleHeight - 1
  172.         For ix_out = 0 To pic_to.ScaleWidth - 1
  173.             ' Map the pixel value from
  174.             ' (ix_out, iy_out) to (x_in, y_in).
  175.             x_in = from_xmin + (ix_out - to_xmin) / x_scale
  176.             y_in = from_ymin + (iy_out - to_ymin) / y_scale
  177.  
  178.             ' Interpolate to find the pixel's value.
  179.             ' Find the nearest integral position.
  180.             ix_in = Int(x_in)
  181.             iy_in = Int(y_in)
  182.  
  183.             ' See if this is out of bounds.
  184.             If (ix_in < 0) Or (ix_in > ix_max) Or _
  185.                (iy_in < 0) Or (iy_in > iy_max) _
  186.             Then
  187.                 ' The point is outside the image.
  188.                 ' Use white.
  189.                 result_pixels(ix_out, iy_out) = white_pixel
  190.             Else
  191.                 ' The point lies within the image.
  192.                 ' Calculate its value.
  193.                 dx1 = x_in - ix_in
  194.                 dy1 = y_in - iy_in
  195.                 dx2 = 1# - dx1
  196.                 dy2 = 1# - dy1
  197.  
  198.                 With result_pixels(ix_out, iy_out)
  199.                     ' Calculate the red value.
  200.                     v11 = input_pixels(ix_in, iy_in).rgbRed
  201.                     v12 = input_pixels(ix_in, iy_in + 1).rgbRed
  202.                     v21 = input_pixels(ix_in + 1, iy_in).rgbRed
  203.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbRed
  204.                     .rgbRed = _
  205.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  206.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  207.         
  208.                     ' Calculate the green value.
  209.                     v11 = input_pixels(ix_in, iy_in).rgbGreen
  210.                     v12 = input_pixels(ix_in, iy_in + 1).rgbGreen
  211.                     v21 = input_pixels(ix_in + 1, iy_in).rgbGreen
  212.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbGreen
  213.                     .rgbGreen = _
  214.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  215.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  216.  
  217.                     ' Calculate the blue value.
  218.                     v11 = input_pixels(ix_in, iy_in).rgbBlue
  219.                     v12 = input_pixels(ix_in, iy_in + 1).rgbBlue
  220.                     v21 = input_pixels(ix_in + 1, iy_in).rgbBlue
  221.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbBlue
  222.                     .rgbBlue = _
  223.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  224.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  225.                 End With
  226.             End If
  227.         Next ix_out
  228.     Next iy_out
  229.  
  230.     ' Set pic_to's pixels.
  231.     SetBitmapPixels pic_to, bits_per_pixel, result_pixels
  232.     pic_to.Picture = pic_to.Image
  233. End Sub
  234.  
  235.